package com.baidu.health.util;

import com.xxl.job.core.executor.impl.XxlJobSimpleExecutor;
import com.xxl.job.core.executor.impl.XxlJobSimpleExecutor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ComponentScan("com.baidu.health.xxl")
public class XxljobConfig {
    private Logger logger= LoggerFactory.getLogger(XxljobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.executor.appname}")
    private String appName;
    @Value("${xxl.job.executor.ip}")
    private String ip;
    @Value("${xxl.job.executor.port}")
    private int port;
    @Value("${xxl.job.accessToken}")
    private String accessToken;
    @Value("${xxl.job.executor.logpath}")
    private String logPath;
    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobSimpleExecutor xxlJobSimpleExecutor(){
        logger.info(">>>>>>>> xxl-job confug init.");
        XxlJobSimpleExecutor xxlJobSimpleExecutor=new XxlJobSimpleExecutor();
        xxlJobSimpleExecutor.setAdminAddresses(adminAddresses);
        xxlJobSimpleExecutor.setAppname(appName);
        xxlJobSimpleExecutor.setIp(ip);
        xxlJobSimpleExecutor.setPort(port);
        xxlJobSimpleExecutor.setAccessToken(accessToken);
        xxlJobSimpleExecutor.setLogPath(logPath);
        xxlJobSimpleExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSimpleExecutor;
    }
}
